package com.qidianit.mes.md.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.md.mapper.WorkstationMapper;
import com.qidianit.mes.md.service.WorkstationService;
import com.qidianit.mes.pojo.Workstation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 奇幻夜之梦
 * @Company 起点编程
 * @Description
 * @Time 2024/10/25 23:04
 */
@Service
public class WorkstationServiceImpl implements WorkstationService {
    @Autowired
    private WorkstationMapper workstationMapper;
    @Override
    public MESResult selectByPage(Workstation workstation, Integer page, Integer limit) {
        //开启分页  参数1： 页数  参数2： 每页大小
        PageHelper.startPage(page,limit);
        //查询全部
        List<Workstation> workstations = workstationMapper.selectByPage(workstation);
        //创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<Workstation> pageInfo = new PageInfo<>(workstations);
        // 将pageInfo对象设置到QdResult中
        return MESResult.success(pageInfo);// 返回结果
    }

    @Override
    public MESResult save(Workstation workstation) {
        if (workstation.getWorkstationCode() == null || workstation.getWorkstationCode() == "") {
            return MESResult.error("工作站编码不能为空");
        }
        if (workstation.getWorkstationName() == null || workstation.getWorkstationName() == "") {
            return MESResult.error("工作站名称不能为空");
        }
        workstation.setCreateTime(new Date());
        workstation.setModifyTime(new Date());
        return workstationMapper.save(workstation) == 1 ? MESResult.success(workstation):MESResult.error();
    }

    @Override
    public MESResult updateWorkstation(Workstation workstation) {
        if (workstation.getWorkstationCode() == null || workstation.getWorkstationCode() == "") {
            return MESResult.error("工作站编码不能为空");
        }
        if (workstation.getWorkstationName() == null || workstation.getWorkstationName() == "") {
            return MESResult.error("工作站名称不能为空");
        }
        if (workstation.getWorkShopId() == null) {
            return MESResult.error("车间必须选择");
        }
        if (workstation.getProcessId() == null) {
            return MESResult.error("工序必须选择");
        }
        return workstationMapper.updateById(workstation) == 1?MESResult.success():MESResult.error();
    }

    @Override
    public MESResult selectById(Long workstationId) {
        return MESResult.success(workstationMapper.selectById(workstationId));
    }

    @Override
    public MESResult delete(Long workstationId) {
        // 修改状态
        return MESResult.isSuccess(workstationMapper.deleteById(workstationId));
    }

    @Override
    public MESResult deletes(String[] ids) {
        return MESResult.success(Constant.RESPONSE_STATUS_OK,"OK",workstationMapper.updateWorkstationsByIds(ids,Constant.DATA_STATUS_DELETE));
    }

    @Override
    public MESResult listWorkstations() {
        return MESResult.success(workstationMapper.listWorkstations());
    }
}

